﻿// /************************************************************
// *                                                           *
// *   Mobile Touch Camera                                     *
// *                                                           *
// *   Created 2015 by BitBender Games                         *
// *                                                           *
// *   bitbendergames@gmail.com                                *
// *                                                           *
// ************************************************************/

using UnityEngine;

namespace BitBenderGames
{

    /// <summary>
    /// A little helper script that allows to focus the camera on a transform either
    /// via code, or by wiring it up with one of the many events of the mobile touch camera
    /// or mobile picking controller.
    /// </summary>
    [RequireComponent(typeof(CommanderMobileTouchCamera))]
    public class CameraMoveOut : MonoBehaviourWrapped
    {

        [SerializeField]
        private float transitionDuration = 0.5f;

        private CommanderMobileTouchCamera MobileTouchCamera { get; set; }

        private Vector3 posTransitionStart;
        private Vector3 posTransitionEnd;
        private float timeTransitionStart;
        private bool isTransitionStarted;
        public bool IsMoveEnd { get; private set; }

        public void Awake()
        {
            MobileTouchCamera = GetComponent<CommanderMobileTouchCamera>();
            isTransitionStarted = false;
            IsMoveEnd = false;
        }

        public void LateUpdate()
        {

            if (MobileTouchCamera.IsDragging || MobileTouchCamera.IsPinching)
            {
                timeTransitionStart = Time.time - transitionDuration;
            }

            if (isTransitionStarted == true)
            {
                if (Time.time < timeTransitionStart + transitionDuration)
                {
                    UpdatePosition();
                }
                else
                {
                    SetPosition(posTransitionEnd);
                    isTransitionStarted = false;
                    IsMoveEnd = true;
                }
            }
        }

        private void UpdatePosition()
        {
            float progress = (Time.time - timeTransitionStart) / transitionDuration;
            Vector3 positionNew = Vector3.Lerp(posTransitionStart, posTransitionEnd, Mathf.Sin(-Mathf.PI * 0.5f + progress * Mathf.PI) * 0.5f + 0.5f);
            SetPosition(positionNew);
        }

        //
        public void MoveOut(int dir)
        {
            Vector3 targetPosition = Vector3.zero;
            if (Mathf.Approximately(transitionDuration, 0))
            {
                SetPosition(targetPosition);
                return;
            }
            timeTransitionStart = Time.time;
            isTransitionStarted = true;
            posTransitionStart = Transform.position;
            int width = -Screen.width;
            if(dir == 1)
            {
                width = Screen.width * 2;
            }
            Vector3 intersectionScreenCenter = MobileTouchCamera.GetIntersectionPoint(MobileTouchCamera.Cam.ScreenPointToRay(new Vector3(width, Screen.height * 0.5f, 0)));
            intersectionScreenCenter.y = posTransitionStart.y;
            Vector3 focusVector = targetPosition - intersectionScreenCenter;
            posTransitionEnd = posTransitionStart + focusVector;
            posTransitionEnd.y = posTransitionStart.y;
            LateUpdate();
        }

        private void SetPosition(Vector3 newPosition)
        {
            newPosition.y = 0;
            Vector3 camPos = Transform.position;
            switch (MobileTouchCamera.CameraAxes)
            {
                case CameraPlaneAxes.XY_2D_SIDESCROLL:
                    camPos.x = newPosition.x;
                    //camPos.y = newPosition.y;
                    break;
                case CameraPlaneAxes.XZ_TOP_DOWN:
                case CameraPlaneAxes.XZ_Rotation:
                    camPos.x = newPosition.x;
                    camPos.z = newPosition.z;
                    break;
            }
            Transform.position = camPos;
        }
    }
}
